﻿--[[
   @brief    self menu
   @author   hqzheng
   @version  1.0
   @date     2017-04-25
--]]


self_menu = 
{
    menu_id = 0;
    menu_h = 0;
    msgbox_id = 0;

    is_english_ver = false;

    init = function (parent_id)
        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui ~= nil then
            self_menu.is_english_ver = tpfui:Is_English_ver();
        end;

        self_menu.menu_id = parent_id;
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
        if nil == menu_ptr then
            return;
        end
        menu_ptr:RemoveAll();
        -- 个人信息item
        local self_info_id = self_menu.add_self_info_item(menu_ptr);
        ui_utility.set_common_menu_top_item_bkg(self_info_id);

        self_menu.add_line_item(menu_ptr);
        ui_utility.setproperty_by_id(self_info_id, "OnClick", "self_menu.on_click_self_item");
        self_menu.set_self_info(self_info_id);

        local my_games_id = self_menu.add_nor_item(
            menu_ptr, LoadStr(ID_SELF_MENU_MY_GAMES),
            "ONEIMAGE:client_mainframe\\app.png;Stretch;0");
        ui_utility.setproperty_by_id(my_games_id, "OnClick", "self_menu.on_click_my_games_item");
        ui_utility.set_common_menu_nor_item_bkg(my_games_id);

        -- 我的资产
        local my_assets_id = self_menu.add_nor_item(
            menu_ptr, LoadStr(ID_SELF_MENU_MY_ASSETS),
            "ONEIMAGE:client_mainframe\\my_assets.png;Stretch;0");
        ui_utility.setproperty_by_id(my_assets_id, "OnClick", "self_menu.on_click_my_assets_item");
        ui_utility.set_common_menu_nor_item_bkg(my_assets_id);


        -- 消息盒子
        local msg_box_id = self_menu.add_nor_item(
            menu_ptr, LoadStr("ID_TPF_MESSAGE_BOX"),
            "ONEIMAGE:client_mainframe\\msgbox.png;Stretch;0");
        ui_utility.setproperty_by_id(msg_box_id, "OnClick", "self_menu.on_click_msgbox_item");
        ui_utility.set_common_menu_nor_item_bkg(msg_box_id);
        self_menu.msgbox_id = msg_box_id;

       
        -- 个人设置
        local self_setting_id = self_menu.add_nor_item(
            menu_ptr, LoadStr(ID_SELF_MENU_MY_SETTING),
            "ONEIMAGE:client_mainframe\\self_setting.png;Stretch;0");
        ui_utility.setproperty_by_id(self_setting_id, "OnClick", "self_menu.on_click_self_setting_item");
        ui_utility.set_common_menu_nor_item_bkg(self_setting_id);
        self_menu.add_line_item(menu_ptr);

        -- 注销
        local switch_user_id = self_menu.add_nor_item(
            menu_ptr, LoadStr(ID_SELF_MENU_SWITCH_USER),
            "ONEIMAGE:client_mainframe\\switch_user.png;Stretch;0");
        ui_utility.setproperty_by_id(switch_user_id, "OnClick", "self_menu.on_click_switch_user_item");
        ui_utility.set_common_menu_bottom_item_bkg(switch_user_id);
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
        if nil ~= menu_ptr then
            local menu_rc = menu_ptr:GetRectEx(TRUE,TRUE);
            self_menu.menu_h = menu_rc.bottom - menu_rc.top;
        end
    end;

    show = function (parent_id, is_show, off_set_x, off_set_y)
        self_menu.init(parent_id);
        if is_show then
            -- 移动到要显示的位置
            local menu_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
            if nil ~= menu_ptr then
                local menu_rc = menu_ptr:GetRectEx(TRUE,TRUE);
                local menu_w = menu_rc.right - menu_rc.left;
                local menu_h = menu_rc.bottom - menu_rc.top;
                menu_rc.left = off_set_x;
                menu_rc.right = menu_rc.left + menu_w;
                menu_rc.top = off_set_y;
                menu_rc.bottom = menu_rc.top;

                menu_ptr:MoveEx(menu_rc,TRUE);
            end
            self_menu.show_animation(parent_id);
        else
            self_menu.hide_animation(parent_id);
        end
    end;

    add_self_info_item = function (menu_ptr)
        local item_id = 0;
        if nil ~= menu_ptr then
            local const_ui_str = U8toCUIStr("wegame_mainframe\\MenuItem.tpf/TenioUILayout/UIObject/Children/UIObject?pnlSelfInfo");
            item_id = menu_ptr:AppendItem(const_ui_str);
            local vip_id = ui_utility.get_ctrl_id_by_path(item_id, "GIcon");
            local big_v_id = ui_utility.get_ctrl_id_by_path(item_id, "VIcon");
            local agent_id = ui_utility.get_ctrl_id_by_path(item_id, "GAIcon");
            local dev_id = ui_utility.get_ctrl_id_by_path(item_id, "DevIcon");
            ui_utility.setproperty_by_id(vip_id, "Visible", "false");
            ui_utility.setproperty_by_id(big_v_id, "Visible", "false");
            ui_utility.setproperty_by_id(agent_id, "Visible", "false");
            ui_utility.setproperty_by_id(dev_id, "Visible", "false");
            if true == wegame_mainframe_model.is_self_vip then
                ui_utility.setproperty_by_id(vip_id, "Visible", "true");
            elseif true == wegame_mainframe_model.is_self_big_v then
                ui_utility.setproperty_by_id(big_v_id, "Visible", "true");
            elseif true == wegame_mainframe_model.is_self_game_agent then
                ui_utility.setproperty_by_id(agent_id, "Visible", "true");
            elseif true == wegame_mainframe_model.is_self_dev then
                ui_utility.setproperty_by_id(dev_id, "Visible", "true");
            end
        end
        return item_id;
    end;

    add_line_item = function (menu_ptr)
        local item_id = 0;
        if nil ~= menu_ptr then
            local const_ui_str = U8toCUIStr("wegame_mainframe\\MenuItem.tpf/TenioUILayout/UIObject/Children/UIObject?pnlSelfLine");
            item_id = menu_ptr:AppendItem(const_ui_str);
        end
        return item_id;
    end; 

    add_nor_item = function (menu_ptr, item_name, icon)
        local item_id = 0;
        if nil ~= menu_ptr then
            local const_ui_str = U8toCUIStr("wegame_mainframe\\MenuItem.tpf/TenioUILayout/UIObject/Children/UIObject?btnIconItem");
            item_id = menu_ptr:AppendItem(const_ui_str);
            local item_icon_id = ui_utility.get_ctrl_id_by_path(item_id, "btnIcon");
            if self_menu.is_english_ver then
                ui_utility.setproperty_by_id(item_icon_id, "XOffset", "15");
                ui_utility.setproperty_by_id(item_id, "TextOffsetX", "35");
            end;

            ui_utility.setproperty_by_id(item_id, "Caption", item_name);
            ui_utility.setproperty_by_id(item_icon_id, "Background", icon);
        end
        return item_id;
    end;

    on_click_self_item = function (uilib, control_id)
        base_utility.mainframe_report("platform_menu", "self_menu_click", "self_info");
        local data = {type = "profile", tgp_id = wegame_mainframe_model.self_tgp_id, custom_param = "&from=mainframe"};
        web_utility.open_link(base_utility.table_to_json(data));

        self_menu.hide_animation(self_menu.menu_id);
    end;

    on_click_self_setting_item = function (uilib, control_id)
        base_utility.mainframe_report("platform_menu", "self_menu_click", "self_setting");
        local url = LoadStr("SELF_SETTING_URL");
        if 0 ~= wegame_mainframe_model.self_tgp_id then
            url = url.."&tgpid="..tostring(wegame_mainframe_model.self_tgp_id);
        end;
        self_menu.jump_home_page_sub_page(url);
        self_menu.hide_animation(self_menu.menu_id);
    end;

    on_click_my_games_item = function (uilib, control_id)
        base_utility.mainframe_report("platform_menu", "self_menu_click", "my_games");
        local url = LoadStr("SELF_SET_MYGAME_URL");
        if 0 ~= wegame_mainframe_model.self_tgp_id then
            url = url.."&tgpid="..tostring(wegame_mainframe_model.self_tgp_id);
        end;
        self_menu.jump_home_page_sub_page(url);
        self_menu.hide_animation(self_menu.menu_id);
    end;

    on_click_my_assets_item = function (uilib, control_id)
        base_utility.mainframe_report("platform_menu", "self_menu_click", "my_assets");
        local json_str = '{"type":"my_assets","sub_nav":""}';
        web_utility.open_link(json_str);
    end;

    on_click_switch_user_item = function (uilib, control_id)
        base_utility.mainframe_report("platform_menu", "self_menu_click", "switch_user");
        event_center.send_event(main_menue_event.MAIN_MENUE_SITCH_USER, "");
    end;

    on_click_msgbox_item = function (uilib, control_id)
        base_utility.mainframe_report("platform_menu", "mainframe_btn_click", "msg_box");
        event_center.send_event(group_event.SHOW_MESSAGE_BOX_DIALOG, {});
        self_menu.set_msgbox_redpoint_visible(false);
    end;

    show_animation = function (menu_id)
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(menu_id);
        if nil == menu_ptr then
            return;
        end

        menu_ptr:Show(TRUE);

        local menu_rc = menu_ptr:GetRectEx(TRUE,TRUE);

        -- 开始尺寸
        local resize_start_sz = SIZE:new_local();
        resize_start_sz.cx = menu_rc.right - menu_rc.left;
        resize_start_sz.cy = 0;

        -- 展开后的尺寸
        local resize_end_sz = SIZE:new_local();
        resize_end_sz.cx = menu_rc.right - menu_rc.left;
        resize_end_sz.cy = self_menu.menu_h;


        -- 缩放动画
        animation_utility.init(
            self_menu.menu_id,
            "Self_menu_Resize", animation_utility.ANI_TYPE.ANI_SIZE);
        animation_utility.size_animation(
            self_menu.menu_id, resize_start_sz, resize_end_sz, 1, 0);
    end;

    hide_animation = function (menu_id)
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(menu_id);
        if nil == menu_ptr then
            return;
        end

        if nil ~= menu_ptr then
            menu_ptr:Show(FALSE);
        end
    end;

    on_resize_animation_end = function ()
    end;

    set_self_info = function (item_id)
        local self_head_id = ui_utility.get_ctrl_id_by_path(item_id, "pnlIcon\\HeadIcon");
        local self_nick_id = ui_utility.get_ctrl_id_by_path(item_id, "lblNick");
        local url = wegame_mainframe_model.self_head_url;
        local name = "";
        local user_game_info = ierd_tgp.users_info.User_game_info:new_local();
        local users_info_adapter =
            base_utility.get_component_adapter("IUsers_info_module",
                "ierd_tgp.users_info.GetUsersInfoModuleAdapter();");
        if nil ~= users_info_adapter then
            users_info_adapter:get_self_game_info(user_game_info);
            name = user_game_info.user_nickname;
        end

        if true == wegame_mainframe_model.is_self_vip then
--            if "" ~= wegame_mainframe_model.vip_game_icon then
--                url = wegame_mainframe_model.vip_game_icon;
--            end

            if "" ~= wegame_mainframe_model.vip_game_name then
                name = wegame_mainframe_model.vip_game_name;
            end
        end

        if nil ~= url and "" ~= url then
            url = "ONEIMAGE:client_mainframe\\info_default.png|" .. url .. ";Circle;0;";            
        else
            url = "ONEIMAGE:client_mainframe\\info_default.png;Circle;0";
        end;
        ui_utility.setproperty_by_id(self_head_id, "Background", url);
        base_utility.log(string.format("[self_menu][set_self_info]url:%s", url));

        if nil ~= name and "" ~= name then
            ui_utility.setproperty_by_id(self_nick_id, "Caption",  name);
        end
    end;

    jump_home_page_sub_page = function (url)
        local param = {};
        param.type = "jump_url";
        param.id = GAME_ID.GAEM_ID_HOME_PAGE;
        param.url = url;
        param.op_from = 0;
        local json_str = base_utility.table_to_json(param);
        web_utility.open_link(json_str);
    end;

    jump_game_sub_page = function (data)
        local param = {};
        param.type = "jump_url";
        param.id = data.game_id or data.id;
        param.url = data.url;
        if param.url and 0 ~= wegame_mainframe_model.self_tgp_id then
            param.url = param.url.."&tgpid="..tostring(wegame_mainframe_model.self_tgp_id);
        end;
        param.op_from = 0;
        base_utility.log("[self_menu][jump_game_sub_page]game_id:" .. param.id, ",url:", param.url);
        local json_str = base_utility.table_to_json(param);
        web_utility.open_link(json_str);
    end;

    open_game_detail_page = function (game_id)
        local param = {};
        param.type = "jump_url";
        -- param.sub_page = "GAME_DETAIL";
        param.game_id = game_id;
        param.url = LoadStr("COMMENT_INVITE_GAME_DETAIL")
            .. tostring(game_id);
        param.op_from = 0;
        local json_str = base_utility.table_to_json(param);
        web_utility.open_link(json_str);
    end;

    set_msgbox_redpoint_visible = function (visible)
        local redpoint_id = ui_utility.get_ctrl_id_by_path(self_menu.msgbox_id, "CtnRedPoint");
        if visible then
           ui_utility.setproperty_by_id(redpoint_id, "Visible", "true");
        else
           ui_utility.setproperty_by_id(redpoint_id, "Visible", "false");
        end
        comm_center.send_broadcast("i_brdcst_self_head_redpoint_update", 
                                    { menu_id = self_menu.msgbox_id });
    end;
};


